//wordcnt.c -- 统计字符数、单词数、行数
#include "stdio.h"
#include "ctype.h" //isspace()函数提供原型
#include "stdbool.h" //为bool、true、false提供定义

#define STOP '|'

int main(){

    char c; //输入字符
    char prev;//读取前一个字符
    long n_chars = 0L;//字符数
    int n_words = 0;//单词数
    int n_linses = 0; //行数
    int p_linses = 0;//不完整的行数
    bool inword = false; //如果c在单词中，inword等于true

    printf("输入: \n");
    prev = '\n';//用于识别完整的行
    while((c = getchar()) != STOP){
        n_chars++;
        if(c == '\n'){
            n_linses++;//统计行
        }if(!isspace(c) && !inword){
            inword = true;
            n_words++;;//统计单词
        }if(isspace(c) && inword){
            inword = false;
        }
        prev = c;
    }

    if(prev != '\n'){
        p_linses = 1;
    }

    printf("characters=%ld,words=%d,linses= %d,",n_chars,n_linses,n_words);
    printf("partial lines = %d\n",p_linses);

    return 0;
}
